package com.tomek.ktoryoperator.utils;

import android.database.Cursor;

public abstract class CursorUtils {
	private static final String CRLF = "\r\n";
	private static final String SEPARATOR = "|";

	public static void printCursor(Cursor cursor) {
		System.out.println(cursorToString(cursor));
	}

	public static String cursorToString(Cursor cursor) {
		StringBuilder sb = new StringBuilder();

		int columns = cursor.getColumnCount();
		//int rows = cursor.getCount();

		String[] columnNames = cursor.getColumnNames();

		for (int i = 0; i < columnNames.length; i++) {
			sb.append(columnNames[i]);
			sb.append(SEPARATOR);
		}

		sb.append(CRLF);

		while (cursor.moveToNext()) {
			sb.append("ROW: ");
			sb.append(cursor.getPosition());
			sb.append("|");
			
			for (int i = 0; i < columns; i++) {
				sb.append(cursor.getString(i));
				sb.append(SEPARATOR);
			}

			sb.append(CRLF);
		}

		cursor.close();

		return sb.toString();
	}
}
